package com.jake.sap.controller;

import com.jake.sap.service.AttendanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

import static com.jake.common.constant.KeyConstants.AREA;
import static com.jake.common.constant.KeyConstants.YEAR;

@RestController
@RequestMapping(value = "attendance")
public class AttendanceController {

    @Autowired
    private AttendanceService attendanceService;

    @PostMapping(value = "record")
    public Map<String, Object> getAttendanceData(@RequestBody Map<String, Object> requestMap) {
        if (requestMap.containsKey(AREA)) {
            return attendanceService.attendanceByArea((String) requestMap.get(AREA));
        }
        if (requestMap.containsKey(YEAR)) {
            return attendanceService.attendanceByDate((int) requestMap.get(YEAR));
        }
        Map<String, Object> errorMessage = new HashMap<>();
        errorMessage.put("error", "requestMap's key should contains area or year.");
        return errorMessage;
    }

}
